package com.junjiao.util.java.image;

import java.awt.Dimension;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.junjiao.util.java.Constants;
import com.junjiao.util.java.ImageDispatcher;

/**
 * 生成缩略图
 * 
 * @author jiaojun
 * @email eshore_jiaojun@163.com
 * @version 2.0 2010-6-13
 */
public class ZoomManage {
	private static final Log log = LogFactory.getLog(ZoomManage.class);

	private static Map imgTypes;
	private static Map notExistImgs;
	private static Map notFindImgs;
	private static Map noIconImgs;
	private static Map attachImgs;

	public static Map getImgTypes() {
		return imgTypes;
	}

	public static void setImgTypes(Map imgTypes) {
		ZoomManage.imgTypes = imgTypes;
	}

	public static Map getNotExistImgs() {
		return notExistImgs;
	}

	public static void setNotExistImgs(Map notExistImgs) {
		ZoomManage.notExistImgs = notExistImgs;
	}

	public static Map getNotFindImgs() {
		return notFindImgs;
	}

	public static void setNotFindImgs(Map notFindImgs) {
		ZoomManage.notFindImgs = notFindImgs;
	}

	public static Map getNoIconImgs() {
		return noIconImgs;
	}

	public static void setNoIconImgs(Map noIconImgs) {
		ZoomManage.noIconImgs = noIconImgs;
	}

	public static Map getAttachImgs() {
		return attachImgs;
	}

	public static void setAttachImgs(Map attachImgs) {
		ZoomManage.attachImgs = attachImgs;
	}

	public static String getNotFindImg(String iconType) {
		String path = (String) notFindImgs.get(iconType);
		if (path == null) {
			path = (String) notFindImgs.get(notFindImgs.keySet().iterator()
					.next());
		}
		return path;
	}

	public static String getNotExistImg(String iconType) {
		String path = (String) notExistImgs.get(iconType);
		if (path == null) {
			path = (String) notExistImgs.get(notExistImgs.keySet().iterator()
					.next());
		}
		return path;
	}

	public static String getNoIconImg(String iconType) {
		String path = (String) noIconImgs.get(iconType);
		if (path == null) {
			path = (String) noIconImgs.get(noIconImgs.keySet().iterator()
					.next());
		}
		return path;
	}

	public static String getAttachImgs(String iconType) {
		String path = (String) attachImgs.get(iconType);
		if (path == null) {
			path = (String) attachImgs.get(attachImgs.keySet().iterator()
					.next());
		}
		return path;
	}

	/**
	 * 生成图片缩略图
	 * 
	 * @param filePath
	 *            原图路径(2007/10/15/16/9GZJO8QN1QWX.jpg)
	 * @param type
	 *            配置文件中定义的属性
	 * @return 图片不存在返加null，否则返回生成后文件名(2007/10/15/16/scale_1_9GZJO8QN1QWX.jpg)
	 */
	public static String getZoomImageByPath(String filePath, String type) {
		String newAbsPath = null;
		File file = null; // =ImageDispatcher.findFile(filePath);
		if (file == null) {
			return newAbsPath;
		}
		try {
			Dimension size = (Dimension) imgTypes.get(type);
			if (size == null) {
				log.error("未配置缩略图类型：" + type);
				throw new Exception("未配置缩略图类型：" + type);
			}
			int width = (int) size.getWidth();
			int height = (int) size.getHeight();

			filePath = filePath.replaceAll("\\\\", "/");
			int nameIndex = filePath.lastIndexOf("/");
			StringBuffer sb = new StringBuffer(filePath);
			sb
					.insert(nameIndex + 1, Constants.IMG_HEAD_NAME + "_" + type
							+ "_");
			newAbsPath = sb.toString();
			File toFile = new File(file.getParentFile(), sb.substring(sb
					.lastIndexOf("/")));
			ZoomImage.zoomImage(file, toFile, width, height, false);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newAbsPath;
	}

	/**
	 * 把文件添加水印后写入硬盘
	 * 
	 * @param saveDir
	 * @param fileName
	 * @param data
	 * @throws Exception
	 */
	public static void saveFile(String saveDir, String fileName, byte[] data)
			throws Exception {
		BufferedOutputStream out = null;
		try {
			File f = new File(saveDir);
			if (!f.exists()) {
				if (!f.mkdirs()) {
					log
							.error("------------------------failed to create file dirs------------------");
					throw new Exception("failed to create file dirs");
				}
			}
			WaterMark.createBgMark(data, new FileOutputStream(new File(f,
					fileName)), null);

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 把文件写入硬盘
	 * 
	 * @param saveDir
	 * @param fileName
	 * @param in
	 * @throws Exception
	 */
	public static void saveFile(String saveDir, String fileName, byte[] data,
			String a) throws Exception {
		BufferedOutputStream fout = null;
		ByteArrayInputStream in = new ByteArrayInputStream(data);
		try {
			File f = new File(saveDir);
			if (!f.exists()) {
				if (!f.mkdirs()) {
					log
							.error("------------------------failed to create file dirs------------------");
					throw new Exception("failed to create file dirs");
				}
			}

			FileOutputStream out = new FileOutputStream(new File(f, fileName));
			byte[] b = new byte[1024 * 10];
			fout = new BufferedOutputStream(out);
			while (in.read(b) > 0) {
				out.write(b);
			}
			out.flush();
			out.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (in != null) {
				in.close();
			}
			if (fout != null) {
				fout.close();
			}
		}
	}

}
